﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace NutriFast
{
    public partial class RecetaAdd : Form
    {
        public RecetaAdd()
        {
            InitializeComponent();
        }
       
        private void agregar(object sender, EventArgs e)
        {

            
            if(grid1.Rows.Count>=1){
                int n = grid2.Rows.Add();                
                int index= grid1.CurrentRow.Index;
                grid2.Rows[n].Cells[0].Value = grid1.Rows[index].Cells[0].Value.ToString();
                grid2.Rows[n].Cells[1].Value = grid1.Rows[index].Cells[1].Value.ToString();
                
                grid1.Rows.RemoveAt(grid1.CurrentRow.Index);
                
            }

        }

       
        private void quitar(object sender, EventArgs e)
        {
            
                if (grid2.Rows.Count!=0)
                {                
                    grid2.Rows.RemoveAt(grid2.CurrentRow.Index);
                }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            
              if (txtName.Text != "")
              {
                  if (grid2.Rows.Count > 0)
                  {     
                        dbPeri.AddReceta(new Receta(txtName.Text));

                        if(grid2.Rows.Count>=1){
                            int idR = dbPeri.GetIdReceta(txtName.Text);
                            for (int i = 0; i < grid2.Rows.Count    ; i++) {
                                    try
                                    {
                            
                                        if (dbPeri.AddRecetaFood(new Receta_Food(idR, int.Parse("" + grid2.Rows[i].Cells[0].Value))))
                                            Console.WriteLine("Receta Agregada"+i);
                                       
                                    }
                                    catch (System.Exception ex)
                                    {
                                        Console.WriteLine(ex.Message);
                                    }
                        
                            }//fin del for
                            txtBuscar.ResetText();
                            txtName.ResetText();
                            for (int i = 0; i < grid2.Rows.Count; i++)
                            {
                                grid2.Rows.RemoveAt(i);

                            }
                            for (int j = 0; j < grid1.Rows.Count; j++)
                            {
                                grid1.Rows.RemoveAt(j);
                            }
                        }//fin del if
                    }//fin del otro if
                    else{
                        errorProvider1.SetError(grid2, "Obligatorio");
                    }

               }
               else
              {
                  errorProvider1.SetError(txtName, "Obligatorio");
              }//fin del else
            

        }

        private void txtBuscar_TextChanged(object sender, EventArgs e)
        {
           this.spPeriquinSerarchFoodTableAdapter.Fill(this.dsPeriquinSearchFood.spPeriquinSerarchFood, txtBuscar.Text);
          
        }


       

                

             
    }
}
